package com.compass.service;

import com.compass.constant.Admin;
import com.compass.entity.Project;
import com.compass.entity.ProjectMemberDo;
import com.compass.entity.User;
import com.compass.mapper.ProjectMapper;
import com.compass.util.SessionUtil;
import com.compass.vo.LoginUser;
import com.compass.vo.ResponseMessage;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.websocket.Session;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: li
 * Date: 2019/3/31
 * Time: 17:00
 * PROJECT：compass
 * PACKAGE: com.compass
 *
 * @author:55324936@qq.com To change this template use File | Settings | File Templates.
 */
@Service
@Slf4j
public class ProjectService {
    @Autowired
    ProjectMapper projectMapper;

    /**
     * 新增项目
     **/
    public ResponseMessage addProject(Project project) {
        LoginUser loginUser = SessionUtil.getSessionUser();
        String userName = loginUser.getUserName();
        project.setUserName(userName);
        try {
            int i = projectMapper.addProject(project);
            //插入成功之后i是等于=1的
            if (1 != i) {
                return ResponseMessage.errorResponse("新增项目异常," + Admin.ADMIN_MESSAGE);
            }
        } catch (Exception e) {
            log.error("新增项目异常", e);
            return ResponseMessage.errorResponse("新增项目异常," + Admin.ADMIN_MESSAGE);
        }
        //创建完项目之后，这里加上一条项目成员记录
        ProjectMemberDo projectMemberDo = new ProjectMemberDo();
        projectMemberDo.setProjectId(project.getId());
        Integer userId = loginUser.getId();
        projectMemberDo.setUserId(userId);
        projectMapper.addProjectMember(projectMemberDo);
        return ResponseMessage.successResponse();
    }

    /**
     * 添加项目成员
     *
     * @param projectMemberDo
     **/
    public ResponseMessage addProjectMember(ProjectMemberDo projectMemberDo) {
        try {
            int i = projectMapper.addProjectMember(projectMemberDo);
            if (1 != i) {
                return ResponseMessage.errorResponse("添加项目成员异常," + Admin.ADMIN_MESSAGE);
            }
            return ResponseMessage.successResponse();
        } catch (Exception e) {
            log.error("添加项目成员异常", e);
            return ResponseMessage.errorResponse("添加项目成员异常," + Admin.ADMIN_MESSAGE);
        }
    }

    /**
     * 获取项目列表，前提是有权限,当你是项目成员的时候你是可以看见项目列表
     **/
    public ResponseMessage getProject(Project project) {
        LoginUser loginUser=SessionUtil.getSessionUser();
        Integer userId = loginUser.getId();
        try {
            Page page = PageHelper.startPage(project.getPageNum(), project.getPageSize(), true);
            List<Project> projectList = projectMapper.getProjectList(userId);
            Long total = page.getTotal();
            Map map = new HashMap();
            /**如果某个用户没有项目的情况下，默认给一个项目**/
            if(0 ==total){
                String userName =  loginUser.getUserName();
                project.setProjectName(userName+"的默认项目");
                this.addProject(project);
                List<Project> projects=new ArrayList<>();
                projects.add(project);
                map.put("total", 1);
                map.put("rows", projects);
                loginUser.setLastProjectId(project.getId());
            }
            map.put("total", total);
            map.put("rows", projectList);
            return ResponseMessage.successResponse(map);
        } catch (Exception e) {
            log.error("查询项目异常", e);
        }
        return ResponseMessage.successResponse();
    }

    /**
     * 获取项目列表，前提是有权限,当你是项目成员的时候你是可以看见项目列表
     **/
    public ResponseMessage getProjectMember(ProjectMemberDo projectMemberDo) {
        try {
            Page page = PageHelper.startPage(projectMemberDo.getPageNum(), projectMemberDo.getPageSize(), true);
            List<User> projectMemberList = projectMapper.getProjectMemberList(projectMemberDo);
            List<LoginUser> loginUserList = new ArrayList<>();
            projectMemberList.forEach(user -> {
                LoginUser loginUser = new LoginUser();
                BeanUtils.copyProperties(user, loginUser);
                loginUserList.add(loginUser);
            });
            Long total = page.getTotal();
            Map map = new HashMap();
            map.put("total", total);
            map.put("rows", loginUserList);
            return ResponseMessage.successResponse(map);
        } catch (Exception e) {
            log.error("查询项目成员异常", e);
        }
        return ResponseMessage.successResponse();
    }

    /**
     * 新注册用户默认创建一个项目
     *
     * @param user
     **/
    public void initProject(User user) {
        Project project = new Project();
        project.setProjectName(user.getUserName() + "的默认项目");
        project.setUserName(user.getUserName());
        projectMapper.addProject(project);
    }


}
